﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AutoCheckGuildWar;
using System.IO;
using Intorise.Common;

namespace WowAnswerHacker
{
    public partial class DictForm : Form
    {
        public DictForm()
        {
            InitializeComponent();
        }

        private void DictForm_Load(object sender, EventArgs e)
        {
            var filenames = Program.CheckConfig.Dicts.Split(',');
            var files = Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory, "*.dict");

            foreach (string s in files)
            {
                FileInfo fileInfo = new FileInfo(s);
                this.checkedListBox1.Items.Add(fileInfo.Name, filenames.Contains(fileInfo.Name));
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            List<string> list = new List<string>();
            foreach (object o in this.checkedListBox1.CheckedItems)
            {
                list.Add(o.ToString());
            }

            Program.CheckConfig.Dicts = string.Join(",", list);
            Program.CheckConfig.Save();
            Program.AnswerDict.Clear();

            list.ForEach(x =>
            {
                FileInfo file = new FileInfo(AppDomain.CurrentDomain.BaseDirectory + "\\" + x);
                if (!file.Exists)
                {
                    ConsoleBLL.WriteLineWaring("字典文件" + file.FullName + "不存在！");
                }
                StreamReader reader = file.OpenText();
                string text = reader.ReadToEnd();
                reader.Close();
                var words = text.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
                Program.AnswerDict.AddRange(words);
            });
            ConsoleBLL.WriteLineInfo("字典文件加载完毕！");
            this.Close();
        }

        public static void LoadDict()
        {
            Program.AnswerDict.Clear();

            var filenames = Program.CheckConfig.Dicts.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
            List<string> list = new List<string>(filenames);

            list.ForEach(x =>
            {
                FileInfo file = new FileInfo(AppDomain.CurrentDomain.BaseDirectory + "\\" + x);
                if (!file.Exists)
                {
                    ConsoleBLL.WriteLineWaring("字典文件" + file.FullName + "不存在！");
                }
                StreamReader reader = file.OpenText();
                string text = reader.ReadToEnd();
                reader.Close();
                var words = text.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
                Program.AnswerDict.AddRange(words);
            });
            ConsoleBLL.WriteLineInfo("字典文件加载完毕！");
        }
    }
}
